﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows;
using System.ServiceModel.DomainServices.Client;
using System.Collections;
using System.Windows.Data;

namespace MVVMS
{
    /// <summary>
    /// Standard ViewModel Implementation for MVVM-S implementation
    /// </summary>
    public class ViewModel : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Implementation

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        /// <summary>
        /// Sends any action to the UI thread, not needed for simple implementations,
        /// but useful when you use a timer or other background worker to
        /// to load data, etc.
        /// </summary>
        /// <param name="action"></param>
        protected void CallOnUI(Action action)
        {
            UIDispatcher.CallOnUI(action);
        }

        protected ICollectionView CreateView(IEnumerable source)
        {
            CollectionViewSource cvs = new CollectionViewSource();
            cvs.Source = source;
            return cvs.View;
        }

        /// <summary>
        /// Override this Method in your ViewModel class to handle
        /// Operations with Errors
        /// </summary>
        /// <param name="opWithErr"></param>
        protected virtual void OnOperationError(OperationBase opWithErr)
        {

        }

        #region IsBusy And BusyMessage Implementation
        bool _IsBusy;
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                if (_IsBusy != value)
                {
                    _IsBusy = value;
                    RaisePropertyChanged("IsBusy");
                }
            }
        }

        string _BusyMessage;
        public String BusyMessage
        {
            get { return _BusyMessage; }
            set
            {
                if (_BusyMessage != value)
                {
                    _BusyMessage = value;
                    RaisePropertyChanged("BusyMessage");
                }
            }
        }
        #endregion
    }
}
